package com.knight.concurrent.chapter6;

/**
 * 通过interrupt方式停止线程
 *
 * @author: knight
 * @Date: 2019-03-01 7:03
 */
public class ThreadCloseGraceful2 {

    public static void main(String[] args) {
        ThreadCloseGraceful2.WorkThread workThread = new ThreadCloseGraceful2.WorkThread();
        workThread.start();

        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        workThread.interrupt();

    }

    private static class WorkThread extends Thread {

        @Override
        public void run() {
            while (true) {
                if (Thread.interrupted()) {
                    break;
                }
            }
            System.out.println("线程结束");
        }
    }
}
